﻿using System;
using System.Web;
using System.Text.RegularExpressions;

namespace Geotrackable.If.ContentModule
{
    public class GeotrackableIfContentModule : IHttpModule
    {
        public GeotrackableIfContentModule()
        {
        }

        public String ModuleName
        {
            get { return "GeotrackableIfContentModule"; }
        }

        // In the Init function, register for HttpApplication 
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {
            application.BeginRequest +=
                (new EventHandler(this.Application_BeginRequest));
            application.EndRequest +=
                (new EventHandler(this.Application_EndRequest));
        }
        public enum EnvironmentType
        {
            Production,
            Development
        }
        private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            String filePath = context.Request.FilePath;
            String filePathAndQuery = context.Request.Url.PathAndQuery;


            if (filePathAndQuery.ToLower() == "/robots.txt")
            {
                return;
            }


            EnvironmentType environment = (context.Request.Url.Host.ToLower() == "if.gs") ? EnvironmentType.Production : EnvironmentType.Development;
            String privateKeyDomain = (environment == EnvironmentType.Production) ? "http://geotrackable.mobi" : "http://dev.geotrackable.mobi";
            String publicKeyDomain = (environment == EnvironmentType.Production) ? "https://geotrackable.org" : "http://dev.geotrackable.org";

            if ("|/|/default.aspx|/default.htm|/default.html|/index.aspx|/index.htm|/index.html".Contains(filePath.ToLower()))
            {
                context.Response.RedirectPermanent(publicKeyDomain);
            }
            else
            {
                String code = Regex.Replace(filePathAndQuery, "[^a-zA-Z0-9-]", String.Empty, RegexOptions.IgnoreCase);
                if (code.Contains("-"))
                {
                    context.Response.Redirect(String.Format("{0}/CodeRequest.aspx?q={1}", publicKeyDomain, code));
                }
                else
                {
                    context.Response.Redirect(String.Format("{0}/trackable/scan.html?c={1}", privateKeyDomain, code));
                }
            }
        }

        private void Application_EndRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            String filePath = context.Request.FilePath;
            String fileExtension = VirtualPathUtility.GetExtension(filePath);
            if (!".axd.aspx".Contains(fileExtension) && filePath.ToLower().Substring(0, 7) != "/admin/")
            {

            }
        }

        public void Dispose() { }
    }
}